package com.ms.game.gmtool.async;

import java.util.ArrayList;

import com.ms.game.bank.BuyFeeProcessor;
import com.ms.game.gmtool.PurchasePlatformType;
import com.ms.game.gmtool.message.SIapAskCashResponse;
import com.ms.game.purchase.bin.PurchaseType;
import com.ms.player.Player;
import com.ms.service.error.ErrorCode;
import com.ms.service.module.datacenterlite.CentralAsyncCommand;
import com.xpec.c4.game.character.PlayerManager;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.common.PlayerCommonInfoManager;

public class IapBillingBuyFeeAsync extends CentralAsyncCommand{

	/**
	 * 
	 */
	private static final long serialVersionUID = 5056922463834321006L;

	private GUID playerId;
		
	private PurchasePlatformType platformType;
	
	private int messageType;
	
	private int purchaseType;
	
	private int addVipExp;
	
	private String iapOrderId;
	
	private String orderId;
	
	private ArrayList<GUID> accountPlayers;
	
	@Override
	public void asyncExecute() throws Exception {
		SIapAskCashResponse response = new SIapAskCashResponse();

		Player player = PlayerManager.getInstance().loadPlayerFromDB(playerId, this.getCurrentModuleServer());
		GUID sessionId = PlayerCommonInfoManager.getInstance().getSessionByPlayerGuid(player.getGuid());

		int canBuy = BuyFeeProcessor.checkCanBuyFee(player, purchaseType);
		
		if (canBuy != ErrorCode.SUCCESS) {
			response.setErrorCode(canBuy);
			MessageAction.sendMessage(sessionId, messageType, response);
			return;
		}
		
		// 包X
		int result = BuyFeeProcessor.buyFee(orderId, player,accountPlayers ,purchaseType, addVipExp);
		if(result != ErrorCode.SUCCESS){
			response.setErrorCode(result);
			MessageAction.sendMessage(sessionId, messageType, response);
			return;
		}

		// 做完回給client
		long feeEndTime = 0l;
		int vip = player.getVip();
		long vipExp = player.getVipExp();
		if (purchaseType == PurchaseType.MONTH) {
			feeEndTime = player.getMonthlyFeeEndTime();
		} else if (purchaseType == PurchaseType.WEEK) {
			feeEndTime = player.getWeeklyFeeEndTime();
		}

		response.setErrorCode(ErrorCode.SUCCESS);
		response.setPurchaseType(purchaseType);
		response.setFeeEndTime(feeEndTime);
		response.setIapOrderId(iapOrderId);
		response.setVip(vip);
		response.setVipExp(vipExp);

	
		MessageAction.sendMessage(sessionId, messageType, response);
		
	}

	@Override
	public void callBackExecute(Object... userData) throws Exception {
		
	}
	
	public int getPurchaseType() {
		return purchaseType;
	}

	public void setPurchaseType(int purchaseType) {
		this.purchaseType = purchaseType;
	}

	public int getAddVipExp() {
		return addVipExp;
	}

	public void setAddVipExp(int addVipExp) {
		this.addVipExp = addVipExp;
	}

	public PurchasePlatformType getPlatformType() {
		return platformType;
	}

	public void setPlatformType(PurchasePlatformType platformType) {
		this.platformType = platformType;
	}

	public String getIapOrderId() {
		return iapOrderId;
	}

	public void setIapOrderId(String iapOrderId) {
		this.iapOrderId = iapOrderId;
	}

	public int getMessageType() {
		return messageType;
	}

	public void setMessageType(int messageType) {
		this.messageType = messageType;
	}

	public GUID getPlayerId() {
		return playerId;
	}

	public void setPlayerId(GUID playerId) {
		this.playerId = playerId;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public ArrayList<GUID> getAccountPlayers() {
		return accountPlayers;
	}

	public void setAccountPlayers(ArrayList<GUID> accountPlayers) {
		this.accountPlayers = accountPlayers;
	}

}
